package SellerAgent;

import Ontologies.Transaction;
import Ontologies.TreeWithPrice;

import javax.swing.table.AbstractTableModel;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: jiaka_000
 * Date: 13-5-18
 * table mode used to show transaction list
 */
public class TransactionTableModelSellerView extends AbstractTableModel {

    private List<List<Object>> data = new ArrayList<List<Object>>();
    private List<String> columnNames = new ArrayList<String>();

    public TransactionTableModelSellerView(List<Transaction> transactionList) {
        super();
        columnNames.add("Common Name");
        columnNames.add("Unit Price");
        columnNames.add("Quantity");
        columnNames.add("Buyer");
        columnNames.add("DateTime");

        for (Transaction transaction:transactionList) {
            addRow(transaction);
        }
    }

    @Override
    public int getColumnCount() {
        return columnNames.size();
    }

    @Override
    public String getColumnName(int column) {
        return columnNames.get(column);
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return data.get(rowIndex).get(columnIndex);
    }

    public void removeRow(int row) {
        data.remove(row);
    }

    public void addRow(Transaction transaction) {
        List<Object> tmp = new ArrayList<Object>();
        tmp.add(transaction.getTree().getCommonName());
        tmp.add(transaction.getTree().getUnitPrice());
        tmp.add(transaction.getTree().getQuantity());
        tmp.add(transaction.getBuyerName());

        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd HH:mm"); //set date formate
        String dateTime = dateFm.format(transaction.getTransactionDate());
        tmp.add(dateTime);
        data.add(tmp);
    }
}
